#include "stm32f10x.h"                  // Device header
#include <stdio.h>

uint8_t USART_TxPacket[4];
uint8_t USART_RxPacket[4];
char USART_ReceivePacket[256];

uint8_t USART_Flag;

void My_USART_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);

	USART_Cmd(USART1, ENABLE);
}

void My_USART_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET);
}

void My_USART_SendArray(uint8_t *arr, uint16_t len)
{
	uint16_t i;
	
	for (i = 0; i < len; i++)
	{
		My_USART_SendByte(arr[i]);
	}
}

void My_USART_SendString(char *arr)
{
	uint16_t i;
	
	for (i = 0; arr[i] != '\0'; i++)
	{
		My_USART_SendByte(arr[i]);
	}
}

uint16_t My_USART_GetNumlen(uint16_t X, uint16_t Y)
{
	uint16_t result = 1;
	
	while (Y--)
	{
		result *= X;
	}
	
	return result;
}

void My_USART_SendNumber(uint16_t Num, uint8_t Lenth)
{
	uint8_t i;
	
	for (i = 0; i < Lenth; i++)
	{
		My_USART_SendByte((Num / My_USART_GetNumlen(10, Lenth-1-i)) % 10 + '0');
	}
}

int fputc(int ch, FILE *f)
{
	My_USART_SendByte(ch);
	
	return ch;
}

void My_USART_SendPacket(void)
{
	My_USART_SendByte(0xFF);
	My_USART_SendArray(USART_TxPacket, 4);
	My_USART_SendByte(0xFE);
}

void USART1_IRQHandler(void)
{	
	static uint8_t USART_RxState = 0;
	static uint8_t USART_pRxPacket = 0;
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		
		if (USART_RxState == 0 && USART_Flag == 0)
		{
			if (RxData == '@')
			{
				USART_RxState = 1;
				USART_pRxPacket = 0;
			}
		}
		else if (USART_RxState == 1)
		{
			if (RxData == '#')
			{
				USART_RxState = 2;
			}
			else
			{
				USART_ReceivePacket[USART_pRxPacket] = RxData;
				USART_pRxPacket++;				
			}
		}
		else if (USART_RxState == 2)
		{
			if (RxData == '!')
			{
				USART_RxState = 0;
				USART_Flag = 1;
			}
		}
		USART_ReceivePacket [USART_pRxPacket] = '\0';
		
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}


